﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using svs.Mobile.BeyondMedia.FeedCore.RSS.Entities;

namespace svs.Mobile.BeyondMedia.FeedCore.RSS.Aggregators
{
	public static class AggregateFeedmanager
	{

		public static string GenerateContent(Feed feed)
		{
			if(!feed.IsAggregate)
				throw new ApplicationException(string.Format("The feed {0} is not an aggreagate feed!", feed.Name));

			var info = new CategoryAggregate(feed.FeedUri);

			return SaveAggregateFeed(feed, info);
		}

		private static string SaveAggregateFeed(Feed feed, CategoryAggregate aggregate)
		{

			CoreHelper.WriteTraceEntry(string.Format("Generating Aggregate Content for feed: {0} ...", feed.Name));

			List<RssFeedItem> aggregateItems = aggregate.GetAggregateRssItems();

			string savePath = RssFeedCache.GetFeedTempPath(feed.FeedUri);

				XmlTextWriter tw = null;
				try
				{
					tw = new XmlTextWriter(savePath, UTF8Encoding.UTF8);
					tw.WriteStartDocument();

					//<opml version="1.1">
					tw.WriteStartElement("rss");
					tw.WriteAttributeString("version", "2.0");

					tw.WriteStartElement("channel");
					tw.WriteStartElement("title");
					tw.WriteString(feed.Name);
					tw.WriteEndElement(); //title

					//tw.WriteStartElement("pubDate");
					//tw.WriteString(DateTime.UtcNow.ToString("r")); //Mon, 10 Oct 2005 08:51PM PDT
					//tw.WriteEndElement();

					//tw.WriteStartElement("lastBuildDate");
					//tw.WriteString(DateTime.UtcNow.ToString("r"));
					//tw.WriteEndElement();

					tw.WriteStartElement("aggregateFeedInfo");
					tw.WriteString(aggregate.ToUrl().ToString());
					tw.WriteEndElement();

					tw.WriteEndElement(); //</channel>

					foreach (var rssItem in aggregateItems)
					{
						tw.WriteStartElement("item");

						tw.WriteStartElement("title");
						tw.WriteString(rssItem.Title);
						tw.WriteEndElement();

						tw.WriteStartElement("link");
						tw.WriteString(rssItem.Link);
						tw.WriteEndElement();

						tw.WriteStartElement("description");
						tw.WriteString(rssItem.Description);
						tw.WriteEndElement();

						if (rssItem.PubDate.HasValue)
						{
							tw.WriteStartElement("pubDate");
							tw.WriteString(rssItem.OriginalPubDateString);
							tw.WriteEndElement();
						}

						tw.WriteStartElement("originatingFeedID");
						tw.WriteString(rssItem.OriginatingFeedID.ToString());
						tw.WriteEndElement();
						
						tw.WriteEndElement(); //item
					}

					tw.WriteEndElement(); //rss
				}
				catch (Exception ex)
				{
					CoreHelper.WriteLogEntry(string.Format("Unable to save aggregate feed: {0}! Reason: {1}", feed.Name, ex.Message));
					throw;
				}
				finally
				{
					if (tw != null)
						tw.Close();
				}

				CoreHelper.WriteTraceEntry(string.Format("Done Generating Aggregate Content! Generated {0} items", aggregateItems.Count));

			return savePath;
			}
	}
}